package com.example.mybatislearn.common.mapper.mapping;

import com.example.mybatislearn.common.lang.Pair;
import com.example.mybatislearn.common.mapper.converter.customer.SmartTypeConverter;
import com.example.mybatislearn.common.mapper.util.ResolveTypeUtils;

public class SmartFieldMapping {
    private String srcFieldName;

    private String destFieldName;

    private Class srcFieldType;

    private Class destFieldType;

    private SmartTypeConverter typeConverter;

    private SmartFieldMapping(String srcFieldName, String destFieldName) {
        this(srcFieldName, destFieldName, null);
    }

    private SmartFieldMapping(String srcFieldName, SmartTypeConverter typeConverter) {
        this(srcFieldName, null, typeConverter);
    }

    private SmartFieldMapping(String srcFieldName, String destFieldName, SmartTypeConverter typeConverter) {
        this.srcFieldName = srcFieldName;
        if (destFieldName == null) {
            this.destFieldName = srcFieldName;
        } else {
            this.destFieldName = destFieldName;
        }

        if (typeConverter != null) {
            this.typeConverter = typeConverter;
            Pair<Class, Class> srcDestKey = ResolveTypeUtils.resolveTypeConverterKey(typeConverter.getClass());
            this.srcFieldType = srcDestKey.getKey();
            this.destFieldType = srcDestKey.getValue();
        }
    }

    public static SmartFieldMapping create(String srcFieldName, String destFieldName) {
        return new SmartFieldMapping(srcFieldName, destFieldName);
    }

    public static SmartFieldMapping create(String srcFieldName, SmartTypeConverter typeConverter) {
        return new SmartFieldMapping(srcFieldName, typeConverter);
    }

    public static SmartFieldMapping create(String srcFieldName, String destFieldName, SmartTypeConverter typeConverter) {
        return new SmartFieldMapping(srcFieldName, destFieldName, typeConverter);
    }

    public String getSrcFieldName() {
        return srcFieldName;
    }

    public String getDestFieldName() {
        return destFieldName;
    }

    public Class getSrcFieldType() {
        return srcFieldType;
    }

    public Class getDestFieldType() {
        return destFieldType;
    }

    public SmartTypeConverter getTypeConverter() {
        return typeConverter;
    }
}
